{% extends "base.html" %}
{% load static %}
{% load form_utils %}

{% block title %}{{ student.name }}的详情 - ExamPulse{% endblock %}

{% block extra_css %}
    <link rel="stylesheet" href="{% static 'users/css/student-detail.css' %}">
    <link rel="stylesheet" href="{% static 'users/css/subject-scores.css' %}">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css">
{% endblock %}

{% block content %}
    <!-- 使用 container 和标准 row/col 布局 -->
    <div class="container py-4">
        <!-- 页面标题 -->
        <div class="page-header mb-5">
            <h1 class="page-title">
                <i class="bi bi-person-circle"></i> {{ student.name }}的详情
            </h1>
            <a href="{% url 'users:student_list' %}" class="btn btn-outline-secondary">
                <i class="bi bi-arrow-left"></i> 返回列表
            </a>
        </div>

        <div class="row">
            <!-- 左侧列：基础信息 (大屏幕占8/12宽度) -->
            <div class="col-lg-8">
                <!-- 基础信息卡片 -->
                <div class="student-card card-gradient-border">
                    <div class="card-header">
                        <h5 class="card-title">
                            <i class="bi bi-info-circle"></i> 基础信息
                        </h5>
                    </div>
                    <div class="card-body">
                        <div class="info-group">
                            <div class="info-item">
                                <span class="info-label">姓名</span>
                                <span class="info-value">{{ student.name }}</span>
                            </div>
                            <div class="info-item">
                                <span class="info-label">考研战数</span>
                                <span class="info-value">
                                <span class="badge badge-gradual {% if student.attempt_count == '一战' %}bg-primary{% else %}bg-info{% endif %}">
                                    {{ student.attempt_count }}
                                </span>
                            </span>
                            </div>
                            <div class="info-item">
                                <span class="info-label">学历背景</span>
                                <span class="info-value">{{ student.education_background }}</span>
                            </div>
                            <div class="info-item">
                                <span class="info-label">本科院校</span>
                                <span class="info-value">{{ student.undergraduate_school|default:"<span class='text-muted'>未填写</span>"|safe }}</span>
                            </div>
                            <div class="info-item">
                                <span class="info-label">本科专业</span>
                                <span class="info-value">{{ student.undergraduate_major|default:"<span class='text-muted'>未填写</span>"|safe }}</span>
                            </div>
                            <div class="info-item">
                                <span class="info-label">目标院校</span>
                                <span class="info-value">{{ student.target_school|default:"<span class='text-muted'>未设置</span>"|safe }}</span>
                            </div>
                            <div class="info-item">
                                <span class="info-label">目标专业</span>
                                <span class="info-value">{{ student.target_major|default:"<span class='text-muted'>未设置</span>"|safe }}</span>
                            </div>
                            <div class="info-item">
                                <span class="info-label">专业代码</span>
                                <span class="info-value">{{ student.target_code|default:"<span class='text-muted'>未设置</span>"|safe }}</span>
                            </div>
                            <div class="info-item">
                                <span class="info-label">考研类型</span>
                                <span class="info-value">{{ student.exam_type }}</span>
                            </div>
                        </div>

                        <!-- 考试科目区域 -->
                        <div class="exam-subjects">
                            <div class="subject-header">
                                <h6 class="subject-title">
                                    <i class="bi bi-book-half"></i> 考试科目
                                </h6>
                                <button class="btn btn-link p-0 subject-toggle">
                                    <i class="bi bi-chevron-up"></i> 收起
                                </button>
                            </div>
                            <div class="subject-content">
                                <div class="info-group">
                                    <div class="info-item">
                                    <span class="info-label flex items-center">
                                        <i class="bi bi-flag text-red-500 me-1"></i> 政治
                                    </span>
                                        <span class="info-value">
                                        {% if student.has_politics %}
                                            政治
                                            <span class="badge bg-light text-dark ms-2"
                                                  data-bs-toggle="tooltip" data-bs-title="满分100分，全国统考">
                                                ?
                                            </span>
                                        {% else %}
                                            <span class="text-muted">不考</span>
                                        {% endif %}
                                    </span>
                                    </div>
                                    <div class="info-item">
                                    <span class="info-label flex items-center">
                                        <i class="bi bi-globe text-blue-500 me-1"></i> 英语
                                    </span>
                                        <span class="info-value">
                                        {% if student.has_english %}
                                            {{ student.english_type }}
                                            <span class="badge bg-light text-dark ms-2"
                                                  data-bs-toggle="tooltip" data-bs-title="满分100分，全国统考">
                                                ?
                                            </span>
                                        {% else %}
                                            <span class="text-muted">不考</span>
                                        {% endif %}
                                    </span>
                                    </div>
                                    <div class="info-item">
                                    <span class="info-label flex items-center">
                                        <i class="bi bi-calculator text-green-500 me-1"></i> 数学
                                    </span>
                                        <span class="info-value">
                                        {% if student.has_math %}
                                            {{ student.math_type }}
                                            <span class="badge bg-light text-dark ms-2"
                                                  data-bs-toggle="tooltip" data-bs-title="满分150分，全国统考">
                                                ?
                                            </span>
                                        {% else %}
                                            <span class="text-muted">不考</span>
                                        {% endif %}
                                    </span>
                                    </div>
                                    <div class="info-item">
                                    <span class="info-label flex items-center">
                                        <i class="bi bi-book text-purple-500 me-1"></i> 专业课
                                    </span>
                                        <span class="info-value">
                                        {% if student.professional_subject_name %}
                                            {{ student.professional_subject_name }}
                                            {% if student.professional_subject_code %}
                                                ({{ student.professional_subject_code }})
                                            {% endif %}
                                            <span class="badge bg-light text-dark ms-2"
                                                  data-bs-toggle="tooltip"
                                                  data-bs-title="满分150/300分，院校自主命题或统考">
                                                ?
                                            </span>
                                        {% else %}
                                            <span class="text-muted">未设置</span>
                                        {% endif %}
                                    </span>
                                    </div>
                                </div>
                            </div>
                        </div>

                        <div class="mt-4">
                            <a href="{% url 'users:student_edit' student.id %}" class="btn btn-gradual-primary">
                                <i class="bi bi-pencil"></i> 编辑信息
                            </a>
                        </div>
                    </div>
                </div>

                <!-- ==================== 科目成绩汇总区域 ==================== -->
                <div class="student-card card-gradient-border mt-4">
                    <div class="card-header d-flex justify-content-between align-items-center">
                        <h5 class="card-title">
                            <i class="bi bi-journal-text"></i> 科目成绩汇总
                        </h5>
                        <div>
                            <a href="{% url 'analysis:profile_dashboard' %}" class="btn btn-gradual-primary">
                                <i class="bi bi-plus-circle"></i> 用户画像
                            </a>
                            <a href="{% url 'score:score_list' %}" class="btn btn-gradual-primary">
                                <i class="bi bi-plus-circle"></i> 成绩列表
                            </a>
                        </div>
                    </div>
                    <div class="card-body">
                        <!-- 搜索框 -->
                        <div class="score-search-container">
                            <input type="text" class="form-control score-search-input"
                                   placeholder="搜索系列或考试名称...">
                        </div>

                        {% if subject_scores_data %}
                            <div class="subject-scores-container">
                                {% for subject_data in subject_scores_data %}
                                    <div class="subject-section" data-subject="{{ subject_data.key }}">
                                        <div class="subject-header">
                                            <div class="subject-title">
                                                <i class="bi {{ subject_data.icon }} {{ subject_data.color }} me-2"></i>
                                                <span class="fw-semibold">{{ subject_data.name }}成绩</span>
                                                <span class="badge bg-light text-dark ms-2">{{ subject_data.total_records }}次考试</span>
                                            </div>
                                        </div>

                                        <div class="series-list">
                                            {% for series in subject_data.series %}
                                                <div class="series-item" data-series="{{ series.name }}">
                                                    <div class="series-header">
                                                        <div class="series-info">
                                                            <span class="series-name">{{ series.name }}</span>
                                                            <span class="series-count">({{ series.count }}套)</span>
                                                            <span class="series-avg">平均: {{ series.avg_score }}分</span>
                                                        </div>
                                                        <div class="series-actions">
                                                            <button class="btn btn-sm btn-outline-secondary series-toggle"
                                                                    type="button"
                                                                    data-bs-toggle="collapse"
                                                                    data-bs-target="#collapse-{{ subject_data.key }}-{{ forloop.counter }}"
                                                                    aria-expanded="false">
                                                                <i class="bi bi-chevron-down"></i>
                                                            </button>
                                                            <a href="{% url 'score:score_list' %}?exam_series={{ series.name|urlencode }}&subject={{ subject_data.name|urlencode }}"
                                                               class="btn btn-sm btn-outline-primary ms-1"
                                                               data-bs-toggle="tooltip"
                                                               data-bs-title="查看{{ series.name }}系列详情">
                                                                <i class="bi bi-arrow-right"></i>
                                                            </a>
                                                        </div>
                                                    </div>

                                                    <div class="collapse series-details"
                                                         id="collapse-{{ subject_data.key }}-{{ forloop.counter }}">
                                                        <div class="series-records">
                                                            {% for record in series.records %}
                                                                <div class="record-item"
                                                                     data-record-id="{{ record.id }}">
                                                                    <div class="record-info">
                                                                        <span class="record-name">{{ record.exam_name }}</span>
                                                                        <span class="record-date">{{ record.exam_date|date:"Y-m-d" }}</span>
                                                                    </div>
                                                                    <div class="record-score">
                                                                        <span class="score-value">{{ record.score }}分</span>
                                                                        {% if record.duration %}
                                                                            <span class="record-duration">{{ record.duration }}分钟</span>
                                                                        {% endif %}
                                                                    </div>
                                                                </div>
                                                            {% endfor %}
                                                        </div>
                                                    </div>
                                                </div>
                                            {% endfor %}
                                        </div>

                                        {% if not subject_data.series %}
                                            <div class="empty-series">
                                                <i class="bi bi-inbox"></i>
                                                <span class="text-muted">暂无{{ subject_data.name }}成绩记录</span>
                                            </div>
                                        {% endif %}
                                    </div>
                                    {% if not forloop.last %}
                                        <hr class="subject-divider">
                                    {% endif %}
                                {% endfor %}
                            </div>
                        {% else %}
                            <div class="empty-state text-center py-5">
                                <i class="bi bi-journal-x display-4 text-muted"></i>
                                <h5 class="mt-3 text-muted">暂无成绩记录</h5>
                                <p class="text-muted">点击"添加成绩"开始记录您的考试表现</p>
                                <a href="{% url 'score:score_add' %}" class="btn btn-gradual-primary mt-3">
                                    <i class="bi bi-plus-circle"></i> 添加第一个成绩
                                </a>
                            </div>
                        {% endif %}
                    </div>
                </div>
            </div>
            <!-- 左侧列结束 -->

            <!-- 右侧列：往年成绩 + 目标成绩 + 成绩分析 + 系统信息 (大屏幕占4/12宽度) -->
            <div class="col-lg-4">
                <!-- 往年成绩卡片 -->
                {% if student.attempt_count != '一战' %}
                    <div class="student-card card-gradient-border sticky-top top-4 mb-4">
                        <div class="card-header d-flex justify-content-between align-items-center">
                            <h5 class="card-title">
                                <i class="bi bi-clock-history"></i> 往年成绩
                            </h5>
                            <a href="{% url 'users:past_score_add' student.id %}" class="btn btn-gradual-primary">
                                <i class="bi bi-plus"></i> 添加
                            </a>
                        </div>
                        <div class="card-body">
                            {% if past_scores %}
                                <div class="score-list">
                                    {% for score in past_scores %}
                                        {% if score and score.id %}
                                            <div class="score-item"
                                                 data-score="{{ score.total_score }}"
                                                 data-year="{{ score.exam_year }}">
                                                <div class="score-header">
                                                    <h6 class="score-year">{{ score.exam_year }}年考研</h6>
                                                    <div class="score-actions">
                                                        <!-- 编辑按钮 -->
                                                        <a href="{% url 'users:past_score_update' student.id score.id %}"
                                                           class="btn btn-edit"
                                                           data-bs-toggle="tooltip"
                                                           data-bs-title="编辑成绩">
                                                            <i class="bi bi-pencil"></i>
                                                        </a>
                                                        <!-- 删除按钮 -->
                                                        <form action="{% url 'users:past_score_delete' student.id score.id %}"
                                                              method="post" class="d-inline">
                                                            {% csrf_token %}
                                                            <button type="submit"
                                                                    class="btn btn-delete"
                                                                    data-bs-toggle="tooltip"
                                                                    data-bs-title="删除成绩"
                                                                    onclick="return confirm('确定要删除 {{ score.exam_year }}年成绩吗？此操作不可撤销。')">
                                                                <i class="bi bi-trash"></i>
                                                            </button>
                                                        </form>
                                                    </div>
                                                </div>

                                                <!-- 第一行：政治和英语 -->
                                                <div class="score-row mb-2">
                                                    <div class="score-subject-wrapper">
                                                        {% if student.has_politics %}
                                                            <div class="score-subject">
                                                                <i class="bi bi-flag text-red-500 me-1"></i>
                                                                政治:
                                                                <strong>{{ score.politics_score|default:"-" }}分</strong>
                                                            </div>
                                                        {% else %}
                                                            <div class="score-subject empty">
                                                                <i class="bi bi-flag text-gray-400 me-1"></i>
                                                                政治: <span class="text-muted">不考</span>
                                                            </div>
                                                        {% endif %}
                                                    </div>

                                                    <div class="score-subject-wrapper">
                                                        {% if student.has_english %}
                                                            <div class="score-subject">
                                                                <i class="bi bi-globe text-blue-500 me-1"></i>
                                                                {{ student.english_type }}:
                                                                <strong>{{ score.english_score|default:"-" }}分</strong>
                                                            </div>
                                                        {% else %}
                                                            <div class="score-subject empty">
                                                                <i class="bi bi-globe text-gray-400 me-1"></i>
                                                                英语: <span class="text-muted">不考</span>
                                                            </div>
                                                        {% endif %}
                                                    </div>
                                                </div>

                                                <!-- 第二行：数学和专业课 -->
                                                <div class="score-row">
                                                    <div class="score-subject-wrapper">
                                                        {% if student.has_math %}
                                                            <div class="score-subject">
                                                                <i class="bi bi-calculator text-green-500 me-1"></i>
                                                                {{ student.math_type }}:
                                                                <strong>{{ score.math_score|default:"-" }}分</strong>
                                                            </div>
                                                        {% else %}
                                                            <div class="score-subject empty">
                                                                <i class="bi bi-calculator text-gray-400 me-1"></i>
                                                                数学: <span class="text-muted">不考</span>
                                                            </div>
                                                        {% endif %}
                                                    </div>

                                                    <div class="score-subject-wrapper">
                                                        {% if student.professional_subject_name %}
                                                            <div class="score-subject">
                                                                <i class="bi bi-book text-purple-500 me-1"></i>
                                                                {{ student.professional_subject_name }}:
                                                                <strong>{{ score.professional_score|default:"-" }}分</strong>
                                                            </div>
                                                        {% else %}
                                                            <div class="score-subject empty">
                                                                <i class="bi bi-book text-gray-400 me-1"></i>
                                                                专业课: <span class="text-muted">未设置</span>
                                                            </div>
                                                        {% endif %}
                                                    </div>
                                                </div>

                                                <!-- 总分显示 -->
                                                <div class="score-total-wrapper mt-2">
                                                    <span class="badge badge-gradual bg-primary score-total">
                                                        总分: {{ score.total_score }}分
                                                    </span>
                                                    {% if forloop.first and past_scores.count >= 2 %}
                                                        {% with prev_score=past_scores.1 %}
                                                            {% if score.total_score > prev_score.total_score %}
                                                                <span class="badge badge-sm bg-green-100 text-green-800 ms-2">
                                                                    <i class="bi bi-arrow-up"></i> 提升{{ score.total_score|add:prev_score.total_score|floatformat:0 }}分
                                                                </span>
                                                            {% elif score.total_score < prev_score.total_score %}
                                                                <span class="badge badge-sm bg-red-100 text-red-800 ms-2">
                                                                    <i class="bi bi-arrow-down"></i> 下降{{ prev_score.total_score|add:score.total_score|floatformat:0 }}分
                                                                </span>
                                                            {% else %}
                                                                <span class="badge badge-sm bg-gray-100 text-gray-800 ms-2">
                                                                    <i class="bi bi-arrow-right"></i> 持平
                                                                </span>
                                                            {% endif %}
                                                        {% endwith %}
                                                    {% endif %}
                                                </div>

                                                {% if score.notes %}
                                                    <div class="note-container mt-2">
                                                        <button class="btn btn-link note-toggle p-0">
                                                            <i class="bi bi-chevron-down"></i> 查看备注
                                                        </button>
                                                        <div class="note-content d-none">
                                                            {{ score.notes|linebreaksbr }}
                                                        </div>
                                                    </div>
                                                {% endif %}
                                            </div>
                                        {% else %}
                                            <!-- 如果score对象无效，显示警告 -->
                                            <div class="alert alert-warning">
                                                成绩数据不完整或无效
                                            </div>
                                        {% endif %}
                                    {% endfor %}
                                </div>

                                <!-- 成绩统计摘要 -->
                                <div class="stats-card">
                                    <div class="stats-header">
                                        <i class="bi bi-bar-chart-fill"></i> 成绩统计
                                    </div>
                                    <div class="stats-grid">
                                        <div class="stats-item">
                                            <div class="stats-label">最高总分</div>
                                            <div class="stats-value">
                                                {{ score_stats.max_score }}分
                                            </div>
                                        </div>
                                        <div class="stats-item">
                                            <div class="stats-label">平均总分</div>
                                            <div class="stats-value">
                                                {{ score_stats.avg_score }}分
                                            </div>
                                        </div>
                                        <div class="stats-item">
                                            <div class="stats-label">最近一次</div>
                                            <div class="stats-value">
                                                {{ score_stats.latest_score }}分
                                            </div>
                                        </div>
                                        <div class="stats-item">
                                            <div class="stats-label">成绩趋势</div>
                                            <div class="stats-value {{ score_stats.trend_color }}">
                                                <i class="bi {{ score_stats.trend_icon }}"></i> {{ score_stats.trend }}
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            {% else %}
                                <div class="empty-state empty-state-animate">
                                    <i class="bi bi-file-earmark-bar-graph"></i>
                                    <p>暂无往年成绩记录，点击"添加"完善信息</p>
                                </div>
                            {% endif %}
                        </div>
                    </div>
                {% endif %}

                <!-- 目标成绩卡片 -->
                <div class="student-card card-gradient-border sticky-top top-4 mb-4">
                    <div class="card-header d-flex justify-content-between align-items-center">
                        <h5 class="card-title">
                            <i class="bi bi-bullseye"></i> 目标成绩
                        </h5>
                        <a href="{% url 'users:target_score_create' student.id %}" class="btn btn-gradual-primary">
                            <i class="bi bi-plus"></i> 添加
                        </a>
                    </div>
                    <div class="card-body">
                        {% with target_scores=student.target_scores.all %}
                            {% if target_scores %}
                                <div class="target-scores-grid">
                                    {% for target in target_scores %}
                                        <div class="target-score-card" data-subject="{{ target.subject_category }}">
                                            <div class="target-score-header">
                                                <div class="subject-icon">
                                                    {% if target.subject_category == 'politics' %}
                                                        <i class="bi bi-flag"></i>
                                                    {% elif target.subject_category == 'english' %}
                                                        <i class="bi bi-globe"></i>
                                                    {% elif target.subject_category == 'math' %}
                                                        <i class="bi bi-calculator"></i>
                                                    {% else %}
                                                        <i class="bi bi-book"></i>
                                                    {% endif %}
                                                </div>
                                                <div class="subject-info">
                                                    <h6 class="subject-name">{{ target.display_name }}</h6>
                                                    <div class="subject-meta">
                                                        <span class="confidence-level">
                                                            <i class="bi bi-emoji-smile"></i>
                                                            {{ target.confidence_level }}%
                                                        </span>
                                                        <span class="priority-level priority-{{ target.priority }}">
                                                            <i class="bi bi-flag"></i>
                                                            优先级 {{ target.priority }}
                                                        </span>
                                                    </div>
                                                </div>
                                                <div class="target-score-actions">
                                                    <a href="{% url 'users:target_score_update' student.id target.id %}"
                                                       class="btn-action btn-edit"
                                                       data-bs-toggle="tooltip"
                                                       data-bs-title="编辑目标">
                                                        <i class="bi bi-pencil"></i>
                                                    </a>
                                                    <form action="{% url 'users:target_score_delete' student.id target.id %}"
                                                          method="post" class="d-inline">
                                                        {% csrf_token %}
                                                        <button type="submit"
                                                                class="btn-action btn-delete"
                                                                data-bs-toggle="tooltip"
                                                                data-bs-title="删除目标"
                                                                onclick="return confirm('确定要删除 {{ target.display_name }} 的目标成绩吗？')">
                                                            <i class="bi bi-trash"></i>
                                                        </button>
                                                    </form>
                                                </div>
                                            </div>

                                            <div class="target-score-content">
                                                <div class="score-comparison">
                                                    <div class="current-score">
                                                        <div class="score-label">当前水平</div>
                                                        <div class="score-value">{{ target.current_level|floatformat:0 }}</div>
                                                        <div class="score-unit">分</div>
                                                    </div>
                                                    <div class="score-arrow">
                                                        <i class="bi bi-arrow-right"></i>
                                                    </div>
                                                    <div class="target-score">
                                                        <div class="score-label">目标分数</div>
                                                        <div class="score-value">{{ target.realistic_target|floatformat:0 }}</div>
                                                        <div class="score-unit">分</div>
                                                    </div>
                                                </div>

                                                <div class="score-range">
                                                    <div class="range-labels">
                                                        <span>保底 {{ target.safety_target|floatformat:0 }}</span>
                                                        <span>理想 {{ target.ideal_target|floatformat:0 }}</span>
                                                    </div>
                                                    <div class="range-bar">
                                                        <div class="range-track"></div>
                                                        <div class="range-safety" style="width: {{ target.safety_percent }}%"></div>
                                                        <div class="range-realistic" style="width: {{ target.realistic_percent }}%"></div>
                                                        <div class="range-ideal" style="width: {{ target.ideal_percent }}%"></div>
                                                        <div class="range-current" style="left: {{ target.current_percent }}%"></div>
                                                    </div>
                                                </div>

                                                {% if target.progress_to_realistic > 0 %}
                                                    <div class="progress-section">
                                                        <div class="progress-info">
                                                            <span class="progress-label">目标进度</span>
                                                            <span class="progress-percent">{{ target.progress_to_realistic|floatformat:0 }}%</span>
                                                        </div>
                                                        <div class="progress-bar-container">
                                                            <div class="progress-bar-fill" style="width: {{ target.progress_to_realistic }}%"></div>
                                                        </div>
                                                    </div>
                                                {% endif %}

                                                <div class="target-meta">
                                                    <div class="meta-item">
                                                        <i class="bi bi-graph-up"></i>
                                                        <span>难度: {{ target.get_goal_difficulty_display }}</span>
                                                    </div>
                                                    <div class="meta-item">
                                                        <i class="bi bi-clock"></i>
                                                        <span>阶段: {{ target.get_study_phase_display }}</span>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                    {% endfor %}
                                </div>

                                <!-- 目标成绩统计 -->
                                <div class="target-stats-summary">
                                    <div class="stats-grid">
                                        <div class="stat-item">
                                            <div class="stat-icon">
                                                <i class="bi bi-journal-text"></i>
                                            </div>
                                            <div class="stat-content">
                                                <div class="stat-value">{{ target_stats.subject_count }}</div>
                                                <div class="stat-label">科目数量</div>
                                            </div>
                                        </div>
                                        <div class="stat-item">
                                            <div class="stat-icon">
                                                <i class="bi bi-emoji-smile"></i>
                                            </div>
                                            <div class="stat-content">
                                                <div class="stat-value">{{ target_stats.avg_confidence }}%</div>
                                                <div class="stat-label">平均信心</div>
                                            </div>
                                        </div>
                                        <div class="stat-item">
                                            <div class="stat-icon">
                                                <i class="bi bi-exclamation-triangle"></i>
                                            </div>
                                            <div class="stat-content">
                                                <div class="stat-value">{{ target_stats.high_risk_count }}</div>
                                                <div class="stat-label">高风险</div>
                                            </div>
                                        </div>
                                        <div class="stat-item">
                                            <div class="stat-icon">
                                                <i class="bi bi-bullseye"></i>
                                            </div>
                                            <div class="stat-content">
                                                <div class="stat-value">{{ target_stats.total_target_score|floatformat:0 }}</div>
                                                <div class="stat-label">总目标分</div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            {% else %}
                                <div class="empty-state empty-state-animate">
                                    <i class="bi bi-bullseye"></i>
                                    <p>暂无目标成绩设定</p>
                                    <div class="mt-3">
                                        <a href="{% url 'users:target_score_create' student.id %}" class="btn btn-gradual-primary btn-sm">
                                            <i class="bi bi-plus-circle"></i> 创建第一个目标
                                        </a>
                                    </div>
                                </div>
                            {% endif %}
                        {% endwith %}
                    </div>
                </div>

                <!-- 成绩分析卡片 -->
                <div class="student-card card-gradient-border sticky-top top-4 mb-4">
                    <div class="card-header">
                        <h5 class="card-title">
                            <i class="bi bi-bar-chart"></i> 成绩分析
                        </h5>
                    </div>
                    <div class="card-body">
                        <!-- 关键指标 -->
                        <div class="analysis-highlights">
                            <div class="highlight-card primary">
                                <div class="highlight-icon">
                                    <i class="bi bi-speedometer2"></i>
                                </div>
                                <div class="highlight-content">
                                    <div class="highlight-value">{{ target_stats.avg_confidence }}%</div>
                                    <div class="highlight-label">平均信心指数</div>
                                </div>
                            </div>
                            <div class="highlight-card success">
                                <div class="highlight-icon">
                                    <i class="bi bi-check-circle"></i>
                                </div>
                                <div class="highlight-content">
                                    <div class="highlight-value">{{ target_stats.subject_count }}</div>
                                    <div class="highlight-label">已设定目标</div>
                                </div>
                            </div>
                        </div>

                        <!-- 风险评估 -->
                        <div class="risk-assessment">
                            <h6 class="assessment-title">
                                <i class="bi bi-shield-exclamation text-warning me-2"></i>
                                风险评估
                            </h6>
                            <div class="risk-levels">
                                <div class="risk-item {% if target_stats.high_risk_count == 0 %}low-risk{% else %}high-risk{% endif %}">
                                    <div class="risk-indicator"></div>
                                    <div class="risk-info">
                                        <div class="risk-category">高风险科目</div>
                                        <div class="risk-count">{{ target_stats.high_risk_count }}科</div>
                                    </div>
                                </div>
                            </div>
                        </div>

                        <!-- 进度概览 -->
                        <div class="progress-overview">
                            <h6 class="overview-title">
                                <i class="bi bi-graph-up-arrow text-success me-2"></i>
                                备考进度
                            </h6>
                            <div class="progress-stats">
                                <div class="progress-stat">
                                    <div class="stat-circle" data-progress="45">
                                        <svg width="60" height="60" viewBox="0 0 60 60">
                                            <circle cx="30" cy="30" r="27" fill="none" stroke="#e9ecef" stroke-width="6"></circle>
                                            <circle cx="30" cy="30" r="27" fill="none" stroke="#4361ee" stroke-width="6" stroke-linecap="round" transform="rotate(-90 30 30)" stroke-dasharray="169.56" stroke-dashoffset="92.86"></circle>
                                            <text x="30" y="35" text-anchor="middle" font-size="12" font-weight="600" fill="#4361ee">45%</text>
                                        </svg>
                                    </div>
                                    <div class="stat-label">整体进度</div>
                                </div>
                                <div class="progress-stat">
                                    <div class="stat-data">
                                        <div class="stat-value">12</div>
                                        <div class="stat-unit">周</div>
                                    </div>
                                    <div class="stat-label">已备考</div>
                                </div>
                                <div class="progress-stat">
                                    <div class="stat-data">
                                        <div class="stat-value">125</div>
                                        <div class="stat-unit">天</div>
                                    </div>
                                    <div class="stat-label">剩余时间</div>
                                </div>
                            </div>
                        </div>

                        <!-- 行动建议 -->
                        <div class="action-suggestions">
                            <h6 class="suggestions-title">
                                <i class="bi bi-lightbulb text-warning me-2"></i>
                                行动建议
                            </h6>
                            <div class="suggestion-list">
                                {% if target_stats.high_risk_count > 0 %}
                                    <div class="suggestion-item warning">
                                        <i class="bi bi-exclamation-triangle"></i>
                                        <span>关注 {{ target_stats.high_risk_count }} 个高风险科目</span>
                                    </div>
                                {% endif %}
                                {% if target_stats.avg_confidence < 70 %}
                                    <div class="suggestion-item info">
                                        <i class="bi bi-emoji-smile"></i>
                                        <span>提升信心指数至70%以上</span>
                                    </div>
                                {% endif %}
                                <div class="suggestion-item success">
                                    <i class="bi bi-clock"></i>
                                    <span>保持当前学习节奏</span>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

                <!-- 系统信息卡片 -->
                <div class="student-card card-gradient-border sticky-top top-4">
                    <div class="card-header">
                        <h5 class="card-title">
                            <i class="bi bi-server"></i> 系统信息
                        </h5>
                    </div>
                    <div class="card-body">
                        <div class="system-list">
                            <div class="system-item system-item-animate">
                                <span class="system-label">创建时间</span>
                                <span class="system-value">
                                    {{ student.created_at|date:"Y-m-d H:i" }}
                                    <span class="small text-gray-400">
                                        {{ create_time_desc }}
                                    </span>
                                </span>
                            </div>
                            <div class="system-item system-item-animate" style="animation-delay: 0.2s">
                                <span class="system-label">最后更新</span>
                                <span class="system-value">
                                    {{ student.last_updated|date:"Y-m-d H:i" }}
                                    <span class="small {{ update_time_color }}">
                                        {{ update_time_desc }}
                                    </span>
                                </span>
                            </div>
                            <div class="system-item system-item-animate" style="animation-delay: 0.4s">
                                <span class="system-label">数据状态</span>
                                <span class="system-value">
                                    {% if student.name and student.attempt_count and student.target_school and student.target_major and student.has_politics is not None %}
                                        <span class="badge badge-gradual bg-success">
                                            <i class="bi bi-check-circle"></i> 完整
                                        </span>
                                    {% elif student.name and student.attempt_count %}
                                        <span class="badge badge-gradual bg-warning">
                                            <i class="bi bi-exclamation-circle"></i> 待完善
                                        </span>
                                    {% else %}
                                        <span class="badge badge-gradual bg-danger">
                                            <i class="bi bi-x-circle"></i> 不完整
                                        </span>
                                    {% endif %}
                                </span>
                            </div>
                            <div class="system-item system-item-animate" style="animation-delay: 0.6s">
                                <span class="system-label">关联账号</span>
                                <span class="system-value">
                                    {{ student.user.username|default:"<span class='text-gray-400'>未关联</span>"|safe }}
                                    {% if student.user.username %}
                                        <span class="badge bg-gray-100 text-gray-700">
                                            {{ student.user.is_active|yesno:"正常,禁用" }}
                                        </span>
                                    {% endif %}
                                </span>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <!-- 右侧列结束 -->
        </div>
        <!-- row结束 -->
    </div>
    <!-- container结束 -->
{% endblock %}

{% block extra_js %}
    <script src="{% static 'users/js/student-detail.js' %}"></script>
    <script src="{% static 'users/js/subject-scores.js' %}"></script>
{% endblock %}